package com.besall.allbase.view.activity.chipstoollevel4.protocol.InfoParams;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;

import java.util.ArrayList;
import java.util.List;

public class PhoneBook extends DialogFragment {

    public EditText mName;
    public EditText mNumber;
    public TextView mPhoneRecord;
    public Button mAddRecord;
    private ConfigCallBack mConfigCallback;
    public int i = 0;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_phonebook, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        mName = (EditText) view.findViewById(R.id.name);
        mNumber = (EditText) view.findViewById(R.id.number);
        mPhoneRecord = (TextView) view.findViewById(R.id.phonerecord);
        mAddRecord = (Button) view.findViewById(R.id.add);
        mAddRecord.setOnClickListener(mRecordListener);
        mPhoneRecord.setMovementMethod(ScrollingMovementMethod.getInstance());
        mPhoneRecord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                scrollToLastLine(mPhoneRecord);
            }

            private void scrollToLastLine(TextView tv){
                int scrollY = 0;
                if(!TextUtils.isEmpty(tv.getText())){
                    final  int linesCount = tv.getLineCount();
                    if(linesCount > 0){
                        scrollY = Math.max(0,(tv.getLayout().getLineTop(linesCount)-tv.getHeight()));
                    }
                }
                tv.scrollTo(0,scrollY);
            }
        });
        recordList.clear();
        i = 0;
        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }


    public List<PhoneBookBean> recordList = new ArrayList();
    PhoneBookBean mBean = new PhoneBookBean();
    public void addRecord(String name, String number){
        String record = mPhoneRecord.getText().toString().trim();
        record += "\n" + i + ":" + "Name: " + name + " Number: " + number;
        mPhoneRecord.setText(record);
        mBean.setName(name);
        mBean.setNumber(number);
        recordList.add(mBean);
        i++;
    }
    public boolean saveConfig(){
        String finalRecord = mPhoneRecord.getText().toString();
        if (TextUtils.isEmpty(finalRecord)) {
            Toast.makeText(getActivity(), "请先添加数据", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_PHONE_BOOK_ID_VALUE, i);
        return true;
    }

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk(recordList);
                    Log.i("TAG", "onClick: " + recordList.toString());
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };

    private final View.OnClickListener mRecordListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = mName.getText().toString();
            String number = mNumber.getText().toString();
            addRecord(name, number);
        }
    };

    public interface ConfigCallBack {

        void onConfigOk(List<PhoneBookBean> record);

        void onConfigCancel();
    }

}
